/*
 * Created on 2008-3-21
 *
 * TODO 
 */
package jacky.lanlan.song.system;

import jacky.lanlan.song.reflection.ClassUtils;

import java.util.*;

import static org.fest.reflect.core.Reflection.*;

/**
 * Java 语言的一些默认约定。
 * @author Jacky.Song
 */
@SuppressWarnings("unchecked")
public abstract class JavaDefault {

	/**
	 * <b style="color:red">Immutable</b>
	 * <p>
	 * Java 基本类型的默认值映射 [类型Class -> 对应默认值]，immutable
	 */
	public static final Map<Class<?>,Object> primitiveDefault;
		
	
	/**
	 * <b style="color:red">Immutable</b>
	 * <p>
	 * Java 集合类型默认值(空集合，immutable) [类型Class -> 对应集合对象]
	 */
	public static final Map<Class<?>,Object> collectionDefault;

	/**
	 * <b style="color:red">Immutable</b>
	 * <p>
	 * Map with primitive type name as key and corresponding primitive
	 * type as value, for example: "int" -> "int.class".
	 */
	public static final Map<String,Class> primitiveTypeNameMap ;
	
	/**
	 * <b style="color:red">Immutable</b>
	 * <p>
	 * Map with primitive wrapper type as key and corresponding primitive
	 * type as value, for example: Integer.class -> int.class.
	 */
	public static final Map<Class,Class> primitiveWrapperTypeMap;
	
	static {
		Map<Class<?>,Object> map=new HashMap<Class<?>,Object>(12);
		map.put(byte.class, (byte)0);
		map.put(short.class, (short)0);
		map.put(int.class, 0);
		map.put(long.class, 0l);
		map.put(float.class, 0.0f);
		map.put(double.class, 0.0d);
		map.put(boolean.class, false);
		map.put(char.class, (char)0);
		primitiveDefault=Collections.unmodifiableMap(map);
		//
		Map<Class<?>,Object> map2=new HashMap<Class<?>,Object>(8);
		map2.put(List.class, Collections.EMPTY_LIST);
		map2.put(Set.class, Collections.EMPTY_SET);
		map2.put(Map.class, Collections.EMPTY_MAP);
		map2.put(Queue.class, 
				Collections.unmodifiableCollection(new LinkedList<Object>()));
		collectionDefault=Collections.unmodifiableMap(map2);
		
		//借用ClassUtils的Map，因为它是private的，所以只能反射
		primitiveTypeNameMap=Collections.unmodifiableMap(
				staticField("primitiveTypeNameMap").ofType(Map.class).in(ClassUtils.class).get());
		//同上
		primitiveWrapperTypeMap=Collections.unmodifiableMap(
				staticField("primitiveWrapperTypeMap").ofType(Map.class).in(ClassUtils.class).get());
	}
	
}
